/*
 * @lc app=leetcode.cn id=98 lang=c
 *
 * [98] 验证二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

// struct TreeNode
// {
//     int val;
//     struct TreeNode *left;
//     struct TreeNode *right;
// };

bool isValid(struct TreeNode *root, int *lst, int *cur)
{

    if (root == NULL)
    {
        return true;
    }
    
    // printf("> root->val:%d\n", root->val);
    if (!isValid(root->left, lst, cur)){
        return false;
    }

    // printf("root.val:%d, item:%d, pre:%d\n", root->val, *cur, lst[0]);
    if (*cur > 0 && lst[0] >= root->val)
    {
        // printf("> pre:%d\n", lst[0]);
        return false;
    }
    lst[0] = root->val;
    (*cur)++;

    return isValid(root->right, lst, cur);
}

bool isValidBST(struct TreeNode *root)
{
    int **lst = malloc(2 * sizeof(int));
    lst[0] = 0;
    int * cur = malloc(sizeof(int)*1);
    *cur = 0;
    // printf("cur:%d, pre:%d\n", *cur, lst[0]);
    return isValid(root, lst, cur);
}
// @lc code=end
